<?php

namespace Apps\AppApi\Method\Search;


use PhalconPlus\Api\Common\ResultStatus;
use Apps\Common\Models\Activity as ActivityModel;

class Activity extends BaseSearch
{
    public function run()
    {
    	/* $page = intval($this->getDataItem('page',1));
    	$keyword = $this->getDataItem('keyword');
    	$city_code = $this->getDataItem('city_code',023); */
    	$param=$_POST['post_body'];
    	$param=str_replace("}",'',$param);
    	$param=str_replace("{",'',$param);
    	$paramarr=explode(",",$param);
    	foreach ($paramarr as $k=>$v){
    		$tmparr=explode(":",$paramarr[$k]);
    		$tmp=$tmparr[0];
    		$newarrs[$tmp]=$tmparr[1];
    	}
    	//var_dump($newarrs);
    	//exit;
    	foreach ($newarrs as $k=>$v){
    		if($k=='"page"'){
    			$page=str_replace('"','',$v);
    		}elseif($k=='"city_code"'){
    			$city_code=str_replace('"','',$v);
    		}elseif('"keyword"'){
    			$keyword=str_replace('"','',$v);
    		}
    	}
        $cityId = $this->getCityId($city_code);
        if($cityId == 0) {
        	return $this->errorLog(ResultStatus::POST_BODY_PARAM_ERROR, '城市代码错误或城市未开通！');
        }
        if(empty($keyword) || $keyword=='""') {
        	$offset = ($page-1)*10;
        	$list = ActivityModel::query()->where("city_id = $cityId and activity_state = 1")->limit(10,$offset)->execute()->toArray();
        	foreach ($list as $arr){
        		foreach ($arr as $k=>$v){
        			$k=str_replace("activity_","",$k);
        			$arrs[$k]=$v;
        		}
        		$newarr[]=$arrs;
        	}
        	$list=$newarr;
        	foreach ($list as $k=>$v){
        		$list[$k]['cover']="http://img.meelier.com/".$list[$k]['cover'];
        	}
        	$this->setResult($list);
        }else{
	        /* $limit = $this->getConfig()->limit;
	        $offset = ($page-1) * $limit;
	        $data = $this->searchActivity($cityId, $keyword, $limit, $offset);
	        $this->setResult($data); */
        	$offset = ($page-1)*10;
        	$list = ActivityModel::query()->where("city_id = '".$cityId."' and activity_title like '%".$keyword."%' and activity_state = 1")->limit(10,$offset)->execute()->toArray();

        	foreach ($list as $arr){
        		foreach ($arr as $k=>$v){
        			$k=str_replace("activity_","",$k);
        			$arrs[$k]=$v;
        		}
        		$newarr[]=$arrs;
        	}
        	$list=$newarr;
        	foreach ($list as $k=>$v){
        		$list[$k]['cover']="http://img.meelier.com/".$list[$k]['cover'];
        	}
        	$this->setResult($list);
        }
    }


}